package jmu.wj.controller;

import jmu.wj.pojo.Emp;
import jmu.wj.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author jxxyw
 */
@RestController
@RequestMapping("emp")
@CrossOrigin
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;
    @Value("${photo.dir}")
    private String realPath;
    @PostMapping("update")
    public Map<String,Object> update(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息：[{}]",emp.toString());

        Map<String, Object> map =new HashMap<>();
        //头像保存
        try {
            if(photo!=null&&photo.getSize()!=0){
                log.info("头像信息：[{}]",photo.getOriginalFilename());
                String newFileName= UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath,newFileName));
                //设置头像地址
                emp.setPath(newFileName);
            }
            //保存员工信息
            empService.update(emp);
            map.put("state",true);
            map.put("msg","保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","保存失败！");
        }
        return map;
    }
    @GetMapping("findOne")
    public Emp findOne(String id){
        log.info("查询员工信息的id：[{}]",id);
        return empService.finaOne(id);
    }
    @GetMapping("delete")
    public Map<String, Object> delete(String id){
        Map<String, Object> map=new HashMap<>();
        try {
            //删除员工头像
            Emp emp = empService.finaOne(id);
            File file = new File(realPath, emp.getPath());
            if(file.exists()){
                file.delete();
            }

            empService.delete(id);
            map.put("state",true);
            map.put("msg","删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除失败！");
        }
        return map;
    }

    @PostMapping("save")
    public Map<String,Object> save(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息：[{}]",emp.toString());
        log.info("头像信息：[{}]",photo.getOriginalFilename());
        Map<String, Object> map =new HashMap<>();
        //头像保存
        try {
            String newFileName= UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath,newFileName));
            //设置头像地址
            emp.setPath(newFileName);
            //保存员工信息
            empService.save(emp);
            map.put("state",true);
            map.put("msg","保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","保存失败！");
        }
        return map;
    }
    @GetMapping("findAll")
    public List<Emp> findAll(){
        return empService.finaAll();
    }
}
